package com.gopay.webtest.controller;

import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.gopay.webtest.controller.base.BaseController;
import com.gopay.webtest.handler.Trans4030Handler;
import com.gopay.webtest.util.DateUtils;
import com.gopay.webtest.vo.IntTransferVO;
import com.gopay.webtest.xml.XmlDao;

/**
 * 内部资金划转
 * @author xiefei
 *
 */
@RequestMapping("/inttransfer")
@Controller
public class IntTransferController extends BaseController {

    @Resource(name = "xmlDao")
    private XmlDao xmlDao;
    
    @Resource(name = "trans4030Handler")
    private Trans4030Handler trans4030Handler;
    
    private static String CHARSET_GBK = "1";
    
    /**
     * 跳转到划转页面
     * 
     * @return
     */
    @RequestMapping("/apply")
    public ModelAndView apply(){
    	ModelAndView mv = new ModelAndView();
        try {
            Map<String, String> map = xmlDao.readData4Map("int_transfer", "apply");

            if (map.containsKey("gopayServerTime")) {
                map.remove("gopayServerTime");
                map.put("gopayServerTime", DateUtils.format(new Date(), "yyyyMMddHHmmss"));
            }
            if (map.containsKey("tranDateTime")) {
                map.remove("tranDateTime");
                map.put("tranDateTime", DateUtils.format(new Date(), "yyyyMMddHHmmss"));
            }

            mv = new ModelAndView("inttransfer.apply");
            mv.addObject("config", map);
        } catch (Exception e) {
            logger.error("跳转到申请页面", e);
        }
    	return mv;
    }
    
    /**
     * 执行划转
     * 
     * @return
     */
    @RequestMapping("/doApply")
    public ModelAndView doApply(@ModelAttribute IntTransferVO applyVo) {
        ModelAndView mv = new ModelAndView();

        try {

            // 签名
            trans4030Handler.sign(applyVo.getTranCode()+"_"+applyVo.getVersion(), "signType", "signValue", applyVo, getRequest());

        } catch (Exception e) {
            logger.error("执行申请", e);
        }
        // gbk
        if (!CHARSET_GBK.equalsIgnoreCase(applyVo.getCharset())) {
            mv.setViewName("inttransfer.doApply");
        } else {
            mv.setViewName("inttransfer.doApply_gbk");
        }
        //mv.setViewName("inttransfer.doApply");
        mv.addObject("applyVo", applyVo);
        return mv;
    }
}
